package cn.jekun.web.shop.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.component.core.Constants;
import cn.jekun.component.core.bean.gen.TsAccount;
import cn.jekun.component.core.bean.gen.TsOperationCode;
import cn.jekun.web.shop.bean.ShopAccounts;
import cn.jekun.web.shop.service.AccountService;


/**
 * 账户信息查询
 * 
 * @author Mr.Dong
 *
 */
@SuppressWarnings("serial")
public class AccountAction extends ShopBaseAction {
	private static final String AUTH_SHOP_MY_SECURITY="SHOP_MY_SECURITY";//安全设置
	private List<TsOperationCode> resultOperCodeList = new ArrayList<TsOperationCode>();
	private List<ShopAccounts> resultQueryDateList = new ArrayList<ShopAccounts>();
	private ShopAccounts shopAccounts;
	public ShopAccounts getShopAccounts() {
		return shopAccounts;
	}
	public void setShopAccounts(ShopAccounts shopAccounts) {
		this.shopAccounts = shopAccounts;
	}
	public List<ShopAccounts> getResultQueryDateList() {
		return resultQueryDateList;
	}
	public void setResultQueryDateList(List<ShopAccounts> resultQueryDateList) {
		this.resultQueryDateList = resultQueryDateList;
	}
	private String queryDate; //日期查询条件 
	private String operType;  //账户流水类型
	private String beginMoney; //开始金额
	private String endMoney;   //结束金额
	private String beginDate;  //开始日期
	private String endDate;    //结束日期
	private String search_date;
	private String orderId;
	public String getSearch_date() {
		return search_date;
	}
	public void setSearch_date(String search_date) {
		this.search_date = search_date;
	}
	public String getBeginDate() {
		return beginDate;
	}
	public void setBeginDate(String beginDate) {
		this.beginDate = beginDate;
	}
	public String getEndDate() {
		return endDate;
	}
	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
	private String keyRange;   //关键词
	private String keyword_s;  //关键词查询条件
	public String getQueryDate() {
		return queryDate;
	}
	public void setQueryDate(String queryDate) {
		this.queryDate = queryDate;
	}
	public String getOperType() {
		return operType;
	}
	public void setOperType(String operType) {
		this.operType = operType;
	}
	public String getBeginMoney() {
		return beginMoney;
	}
	public void setBeginMoney(String beginMoney) {
		this.beginMoney = beginMoney;
	}
	public String getEndMoney() {
		return endMoney;
	}
	public void setEndMoney(String endMoney) {
		this.endMoney = endMoney;
	}
	public String getKeyRange() {
		return keyRange;
	}
	public void setKeyRange(String keyRange) {
		this.keyRange = keyRange;
	}
	public String getKeyword_s() {
		return keyword_s;
	}
	public void setKeyword_s(String keyword_s) {
		this.keyword_s = keyword_s;
	}
	
	public String getOrderId() {
		return orderId;
	}
	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}
	public List<TsOperationCode> getResultOperCodeList() {
		return resultOperCodeList;
	}
	public void setResultOperCodeList(List<TsOperationCode> resultOperCodeList) {
		this.resultOperCodeList = resultOperCodeList;
	}

	public AccountService getAccountService() {
		return accountService;
	}
	public void setAccountService(AccountService accountService) {
		this.accountService = accountService;
	}
	@Resource(name=AccountService.BEAN_NAME)
	AccountService accountService;

	@SuppressWarnings("unchecked")
	public String getAccountDetails(){
		
		JSONObject jsonObject=new JSONObject();
		
		String shopId=this.getSessionUser().getShopId();
		
		try {
			this.resultList = this.accountService.getAccountDetails(queryDate,operType,beginMoney,endMoney,keyRange,
					beginDate,endDate,
					keyword_s.trim(),shopId,page,orderId);
			this.resultQueryDateList = this.accountService.getQueryDate(queryDate);
			request.setAttribute("beginDate",resultQueryDateList.get(0).getBeginDate()==null?
					"":resultQueryDateList.get(0).getBeginDate().toString());
			request.setAttribute("endDate",resultQueryDateList.get(0).getEndDate()==null?
					"":resultQueryDateList.get(0).getEndDate().toString());	
			if(search_date!=null && "true".equals(search_date.trim())){
				request.setAttribute("beginDate",beginDate);
				request.setAttribute("endDate",endDate);
			}
			TsAccount tempBalance = this.accountService.one(shopId);
			java.text.NumberFormat nf = java.text.NumberFormat.getInstance();   
			nf.setGroupingUsed(false); 
			String balance="";
			if(tempBalance==null){
				balance="0";
			}else{
				balance=nf.format(tempBalance.getBalance());
			}
			
			JSONArray jsonArray = new JSONArray();
			
			HashMap<String, Object> hashMap;
			
			for(int i=0;i<this.resultList.size();i++){
				
				
				ShopAccounts tempShopAccounts =(ShopAccounts) resultList.get(i);
				
				hashMap=new HashMap<String, Object>();
				
				hashMap.put("createTime",tempShopAccounts.getCreateTime()==null?"":tempShopAccounts.getCreateTime());
				
				hashMap.put("accountJourId", tempShopAccounts.getAccountJourId()==null?"":tempShopAccounts.getAccountJourId());
				
				hashMap.put("orderId", tempShopAccounts.getOrderId());
				
				hashMap.put("operName",tempShopAccounts.getOperName()==null?"":tempShopAccounts.getOperName());
				
				hashMap.put("amount",tempShopAccounts.getAmount()==null?"":tempShopAccounts.getAmount());
				
				hashMap.put("balance",tempShopAccounts.getBalance()==null?"":tempShopAccounts.getBalance());
				
				hashMap.put("operCode",tempShopAccounts.getOperCode()==null?"":tempShopAccounts.getOperCode());
				
				
				
				jsonArray.add(hashMap);
				
			}
			
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
			
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");

			jsonObject.put(Constants.Page.PAGE_NUMBER,page.getPageNumber());
			
			jsonObject.put(Constants.Page.PAGE_SIZE,page.getPageSize());
			
			jsonObject.put(Constants.Page.TOTAL_COUNT,page.getTotalCount());
			
			jsonObject.put(Constants.Page.TOTAL_PAGE,page.getPages());
			
			jsonObject.put("balance", balance);

			jsonObject.put("accountListData", jsonArray);
			
			
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
			
		
		return this.writeJson(jsonObject);
		
		
		
		

	}
	/**
	 * 获取提现流水详情
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String getAccountDetailsById(){
	 JSONObject jsonObject=new JSONObject();
		
		try {
			String accountJourId=request.getParameter("accountJourId");
			 this.shopAccounts = accountService.getAccountDetailsById(accountJourId);
			 
				if(shopAccounts!=null){
					jsonObject.put("applystautusId",shopAccounts.getApplyStatusId()==null?"":shopAccounts.getApplyStatusId());
					jsonObject.put("applystautusname",shopAccounts.getApplyStatusName()==null?"":shopAccounts.getApplyStatusName());
					jsonObject.put("settleResultId",shopAccounts.getSettleResultId()==null?"":shopAccounts.getSettleResultId());
					jsonObject.put("settleresult",shopAccounts.getSettleResult()==null?"":shopAccounts.getSettleResult());
					jsonObject.put("accountJourId",shopAccounts.getAccountJourId()==null?"":shopAccounts.getAccountJourId());
					jsonObject.put("accountId",shopAccounts.getAccountId()==null?"":shopAccounts.getAccountId());
					jsonObject.put("applyCommitTime",shopAccounts.getApplycommitTime()==null?"":shopAccounts.getApplycommitTime());
					jsonObject.put("amount",shopAccounts.getAmount()==null?"":shopAccounts.getAmount());
					jsonObject.put("fee",shopAccounts.getFee()==null?"":shopAccounts.getFee());
					jsonObject.put("applyTypeName",shopAccounts.getApplyTypeName()==null?"":shopAccounts.getApplyTypeName());
					jsonObject.put("reqBankTime",shopAccounts.getReqBankTime()==null?"":shopAccounts.getReqBankTime());
					jsonObject.put("operationName",shopAccounts.getOperationName()==null?"":shopAccounts.getOperationName());
					
					jsonObject.put("finishBankTime",shopAccounts.getFinishBankTime()==null?"":shopAccounts.getFinishBankTime());
					jsonObject.put("finishTime",shopAccounts.getFinishTime());
					jsonObject.put("accountName",shopAccounts.getAccountName());
					jsonObject.put("bankNumber",shopAccounts.getBankNumber());
					jsonObject.put("bankName",shopAccounts.getBankName());
					jsonObject.put("createUserName",shopAccounts.getCreateUserName());
					
				}
				
				
				
				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
				jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
				
				
			
			
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		
		return this.writeJson(jsonObject);
	 
	 
	 
	}
	
	
	
	
	/**
	 * 获取流水类型
	 * @return
	 */
	public String queryOperType(){
		
		JSONObject jsonObject=new JSONObject();
		
		
		try {
			this.resultOperCodeList = this.accountService.getOperTypes();
			
			JSONArray jsonArray = new JSONArray();
			
			HashMap<String, Object> hashMap;
			
			for(int i=0;i<this.resultOperCodeList.size();i++){
				
			
				TsOperationCode tempTsOperationCode=(TsOperationCode) resultOperCodeList.get(i);
				
				hashMap=new HashMap<String, Object>();
				
				hashMap.put(TsOperationCode.COL_OPERATION_CODE, tempTsOperationCode.getOperationCode());
				
				hashMap.put(TsOperationCode.COL_OPERATION, tempTsOperationCode.getOperation());
				
				jsonArray.add(hashMap);
				
			}
			
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
			
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");

			jsonObject.put("operTypeListData", jsonArray);
			
			
			
		}  catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		
		return this.writeJson(jsonObject);
	}

}
